package main

import (
	"fmt"
	"sync"
	"time"
)
var wg sync.WaitGroup

func consumer(queue chan int)  {
	defer wg.Done()
	//data:=<-queue //msg值放入data,则此时msg已经空了
	//fmt.Println(data)
	//for data:=range queue{
	//	fmt.Println(data)
	//	time.Sleep(time.Second)
	//}
	for {
		data,ok :=<-queue
		fmt.Println(data,ok)
		time.Sleep(time.Second)
	}
}

func main() {
	var msg chan int
	//msg = make(chan int) //第一种初始化方式：无缓冲
	msg = make(chan int,1) //第二种，带缓冲空间
	msg<-1 //将1放入channe中
	wg.Add(1)
	go consumer(msg)
	msg<-2 //将1放入channe中
	//需要关闭channel 不然会报错
	close(msg)
	wg.Wait()
}
